﻿@model Apps.Models.HC_ITEMS_EXTEND
@using Apps.Web.Core;
@using Apps.Locale;
@using Apps.Models.Sys;
@using Apps.Models.Enum;
@using Apps.Common;
@using Apps.Models;
@{
    ViewBag.Title = "修改";
    Layout = "~/Views/Shared/_Index_LayoutEdit.cshtml";
    List<permModel> perm = null;
    //string fields = string.Empty;
    //var itemsModel = ViewBag.ItemsModel as HC_ITEMS;
    //if (itemsModel != null)
    //{
    //    foreach (var item in itemsModel.fieldList)
    //    {
    //        fields += item.EXTEND_FIELD_ID + ",";
    //    }
    //}
    //var FieldList = ViewBag.FieldList as List<HC_EXTEND_FIELD>;
}

<script type="text/javascript">
    $(function () {
        function GetItemData() {
            var model = {
                ID:$("#ID").val(),
                ITEM_TITLE: $("#ITEM_TITLE").val(),
                ITEM_NAME: $("#ITEM_NAME").val(),
                ENABLE: $("#ENABLE").val(),
                SORT: $("#SORT").val(),
                FIELDS: checkedboxs.join()
            };
            return model;
        }
        $("#btnSave").click(function () {
            if (checkedboxs.length == 0) {
                return window.parent.frameReturnByMes("请至少选择一个化验项");
            }
            if ($("form").valid()) {
                $.ajax({
                    url: "@Url.Action("Edit")",
                    type: "Post",
                    data: GetItemData(),
                    dataType: "json",
                    success: function (data) {
                        if (data.type == 1) {
                            window.parent.frameReturnByMes(data.message);
                            window.parent.frameReturnByReload(false);
                            window.parent.frameReturnByClose()
                        }
                        else {
                            window.parent.frameReturnByMes(data.message);
                        }
                    }
                });
            }
            return false;
        });

        $("#btnReturn").click(function () {
            window.parent.frameReturnByClose();
        });
    });
</script>
<div class="mvctool bgb">
    @Html.ToolButton("btnSave", "fa fa-save", Resource.Save, ref perm, "Save", true)
    @Html.ToolButton("btnReturn", "fa fa-reply", Resource.Reply, false)
    <span><font color="red"> 提示：如报单化验项有变化，为了保证数据的完整性，请修改后重新登录系统</font></span>
</div>
@using (Html.BeginForm())
{
    <input type="hidden" id="fields" value="@Model.FIELDS" />
    @Html.Hidden("ID", Model.ID)
    <table class="formtable">
        <tbody>
            <tr>
                <th style="width:80px">
                    <label for="ITEM_TITLE"><font color="red">*</font>报单名称</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="化产项目名称字段是必需的。" id="ITEM_TITLE" name="ITEM_TITLE" type="text" value="@Model.ITEM_TITLE" autocomplete="off" />
                </td>
                <th style="width:80px">
                    <label for="ITEM_NAME"><font color="red">*</font>报单别名</label>：
                </th>
                <td>
                    <input class="text-box single-line" readonly="readonly" data-val="true" data-val-required="化产项目名称字段是必需的。" id="ITEM_NAME" name="ITEM_NAME" type="text" value="@Model.ITEM_NAME" autocomplete="off" />
                </td>
            </tr>
            <tr>
                <th style="width:80px">
                    <label for="SORT"><font color="red">*</font>排序</label>：
                </th>
                <td>
                    <input class="text-box single-line" id="SORT" name="SORT" type="text" value="@Model.SORT" autocomplete="off" />
                </td>
                <th style="width:80px">
                    <label for="ENABLE"><font color="red">*</font>启用状态</label>：
                </th>
                <td>
                    <select id="ENABLE" name="ENABLE" autocomplete="off"><option @Utils.CheckedSelected(Model.ENABLE.ToString(), "1") value="1">启用</option><option value="0" @Utils.CheckedSelected(Model.ENABLE.ToString(), "0")>禁用</option></select>
                </td>
            </tr>
            <tr>
                <td colspan="5" style="vertical-align: top; ">
                    <table id="List"></table>
                </td>
            </tr>
        </tbody>
    </table>
    @Html.ValidationSummary()
}
<script type="text/javascript">
    var itemIds = $("#ID").val();
    var fields = $("#fields").val();
    var itemId = '';
    var checkboxs = [];
    var checkedboxs = [];
    function GetChecked() {
        var checkedItems = $("#List").datagrid('getChecked');
        $each(checkedItems, function (index, item) {
            alert(item);
            //if (checkboxs[i] != row.ID) {
            //    checkboxs.push(row.ID);
            //    //break;
            //}
        });
    }

    $(function () {
        $('#List').datagrid({
            url: '@Url.Action("GetListDetail")?itemId=@(Model.ID)',
            width: SetGridWidthSub(30),
            methord: 'post',
            height: 550,
            fitColumns: true,
            sortName: 'ID',
            sortOrder: 'desc',
            idField: 'ID',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: true,
            striped: true, //奇偶行是否区分
            rownumbers: true,//行号
            onClickRow: function (index, data) {
                //$("#beginTime").val('');
                //$("#endTime").val('');
                //$("#formState").attr("value", -2);
                var row = $('#List').datagrid('getSelected');
                if (row != null) {
                    alert(row.ItemId)
                }
            },
            onCheck: function (index, row) {
                var rows = $('#List').datagrid('getChecked');
                checkedboxs.push(rows[rows.length - 1].ID);

            },
            onUncheck: function (index, row) {
                if (checkedboxs.length > 0) {
                    for (var i = 0; i < checkedboxs.length; i++) {
                        if (checkedboxs[i] == row.ID) {
                            checkedboxs.splice(i, 1);
                            break;
                        }
                    }
                }
            },
            onLoadSuccess: function (data) {
                var checkedData = data.rows.myfilter(function (item, inde, array) {
                    return item.ID != '' && item.ID != null
                }
                );
                checkboxs = new Array();
                for (var i = 0; i < checkedData.length; i++) {
                    if (fields.indexOf(checkedData[i].ID) != -1) {
                        checkboxs.push(checkedData[i].ID);
                        $('#List').datagrid('checkRow', i);
                    }
                }
            },
            columns: [[
                { field: 'ID', title: 'ID', width: 80, checkbox: true },
                { field: 'ITEM_ID', title: '化验项编号', width: 80, hidden: true },
                { field: 'TITLE', title: '化验项', width: 120, sortable: true },
                { field: 'NAME', title: '别名', width: 90, sortable: true },
                { field: 'REMARK', title: '备注', width: 90 },
                { field: 'BASEVALUE', title: '基值', width: 60, sortable: true, },
                { field: 'MINVALUE', title: '最小值', width: 60, sortable: true, },
                { field: 'MAXVALUE', title: '最大值', width: 60, sortable: true, },
            ]]
        });
    });
    Array.prototype.myfilter = function (fun /*, thisp*/) {
        var len = this.length;
        if (typeof fun != "function") {
            throw new TypeError();
        }
        var res = new Array();
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this) {
                var val = this[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, this)) {
                    res.push(val);
                }
            }
        }
        return res;
    };
</script>
